package com.csdemos;

import com.csdemos.algs.BinaryTree;
import com.csdemos.algs.BinaryTree.Node;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class BinaryTreeDemoActivity extends Activity {
	
	private BinaryTree binaryTree;
	
	private EditText key;
	private TextView tree;
	private TextView result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.binary_tree_demo);
		key = (EditText) findViewById(R.id.key);
		tree = (TextView) findViewById(R.id.tree);
		result = (TextView) findViewById(R.id.result);
		
		binaryTree = new BinaryTree();
		refresh();
	}
	
	private void setResultString(int result) {
		this.result.setText(String.valueOf(result));
	}
	
	private void setResultString(String result) {
		this.result.setText(result);
	}
	
	private void refresh() {
		tree.setText(binaryTree.parenthesisTreeWalk(binaryTree.getRoot()));
	}
	
	public void max(View view) {
		Node node = binaryTree.iterativeTreeMaximum(binaryTree.getRoot());
			if (node == null) {
				setResultString("null");
			} else {
				setResultString(node.value);
			}
			
			refresh();
	}
	
	public void min(View view) {
		Node node = binaryTree.iterativeTreeMinimum(binaryTree.getRoot());
			if (node == null) {
				setResultString("null");
			} else {
				setResultString(node.value);
			}
			
			refresh();
	}
	
	public void succ(View view) {
		try {
			int value = Integer.valueOf(key.getText().toString()).intValue();
			Node node = binaryTree.treeSearch(binaryTree.getRoot(), value);
			if (node == null) {
				setResultString("Node with such key is absent!");
			} else {
				Node node2 = binaryTree.treeSuccessor(node);
				if (node2 == null) {
					setResultString("null");
				} else {
					setResultString(node2.value);
				}
			}
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
	}
	
	public void pred(View view) {
		try {
			int value = Integer.valueOf(key.getText().toString()).intValue();
			Node node = binaryTree.treeSearch(binaryTree.getRoot(), value);
			if (node == null) {
				setResultString("Node with such key is absent!");
			} else {
				Node node2 = binaryTree.treePredecessor(node);
				if (node2 == null) {
					setResultString("null");
				} else {
					setResultString(node2.value);
				}
			}
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
	}
	
	public void intw(View view) {
		try {
			setResultString(binaryTree.inorderTreeWalk(binaryTree.getRoot()));
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
	}
	
	public void pretw(View view) {
		try {
			setResultString(binaryTree.preorderTreeWalk(binaryTree.getRoot()));
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
	}
	
	public void posttw(View view) {
		try {
			setResultString(binaryTree.postorderTreeWalk(binaryTree.getRoot()));
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
	}
	
	public void insert(View view) {
		Node node = new Node(Integer.valueOf(key.getText().toString()).intValue());
		binaryTree.treeInsert(node);
		refresh();
	}
	
	public void delete(View view) {
		int value = Integer.valueOf(key.getText().toString()).intValue();
		Node node = binaryTree.treeSearch(binaryTree.getRoot(), value);
		if (node == null) {
			setResultString("Node with such key is empty!");
		} else {
			binaryTree.treeDelete(node);
		}
		refresh();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_priority_queue_demo, menu);
		return true;
	}

}
